package com.zjw.zy.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.utils.ResourcesUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/11/2 15:17
 */
class ExpandView : BaseViewGroup {

    private val button: SlideButton by lazy { findViewById(R.id.expand_view_enable_button) }
    private val info: TextView by lazy { findViewById(R.id.expand_view_info) }
    private val icon: ImageView by lazy { findViewById(R.id.expand_view_icon) }
    private val title: TextView by lazy { findViewById(R.id.expand_view_title) }
    private val expandContent: FrameLayout by lazy { findViewById(R.id.expand_view_contain) }

    private var mIconId = 0
    private var mTitleId = 0
    private var expandViewId = 0
    private var expandView: View? = null

    private var mListener: Listener? = null

    constructor(context: Context?) : super(context) {

    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initSelf(context, attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {

        initSelf(context, attrs)
    }

    private fun initSelf(context: Context?, attrs: AttributeSet?) {
        context?.let {
            it.obtainStyledAttributes(attrs, R.styleable.ExpandView).apply {
                mIconId = this.getResourceId(R.styleable.ExpandView_icon_drawable_id, 0)
                mTitleId = this.getResourceId(R.styleable.ExpandView_title_id, 0)
                expandViewId = this.getResourceId(R.styleable.ExpandView_expand_view_id, 0)
                this.recycle()
            }
        }
    }

    override fun onLayoutId(): Int {
        return R.layout.enable_view_layout
    }

    override fun onBindListener() {

        button.bindListener(object : SlideButton.Listener {
            override fun onEnable(enable: Boolean) {
                if (enable) {
                    expand()
                } else {
                    unExpand()
                }
            }
        })

    }

    override fun onBindValue() {
        if (mIconId != 0) {
            icon.setImageDrawable(ResourcesUtils.getDrawable(mIconId))
        }

        if (mTitleId != 0) {
            title.text = ResourcesUtils.getString(mTitleId)
        }
    }

    override fun onFindView(rootView: View) {


    }

    private fun initExpandView() {
        if (expandViewId == 0) {
            return
        }
        if (expandView != null) {
            return
        }
        expandView = LayoutInflater.from(context).inflate(expandViewId, expandContent, true)
    }

    fun <T : View> getExpandView() = expandView as T

    fun expand() {
        initExpandView()

        if (expandView == null) {
            return
        }
        if (isExpand()) {
            return
        }
        button.makeEnable(true)

        expandContent.visibility = View.VISIBLE
        mListener?.onExpandChange(true)
    }

    fun unExpand() {
        if (!isExpand()) {
            return
        }
        button.makeEnable(false)
        expandContent.visibility = View.GONE
        mListener?.onExpandChange(false)
    }

    fun addExpandView(view: View) {
        expandView = view
        expandView?.layoutParams = WindowManager.LayoutParams()
        expandContent.addView(expandView)
    }

    fun setInfo(info: String) {
        this.info.text = info
    }

    fun bindListener(listener: Listener) {
        mListener = listener
    }

    fun isExpand() = expandContent.visibility == View.VISIBLE

    interface Listener {
        fun onExpandChange(expand: Boolean)
    }
}
